package com.gxuwz.check.dao.admin.impl;

import com.gxuwz.check.base.BaseDao;
import com.gxuwz.check.dao.admin.ICollegeDao;
import com.gxuwz.check.model.entity.SysCollegeEntity;
import com.gxuwz.check.util.PageBean;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("CollegeDaoImpl")
public class CollegeDaoImpl extends BaseDao implements ICollegeDao {
    @Override
    public List<SysCollegeEntity> listCollege(int page) {
        System.out.println("page College "+page);
        Criteria criteria = getSession().createCriteria(SysCollegeEntity.class);
        criteria.setFirstResult((page - 1)* new PageBean<>().getPageSize());
        criteria.setMaxResults(new PageBean<>().getPageSize());
        List<SysCollegeEntity> list = criteria.list();
        SessionClose();
        return list;
    }

    @Override
    public void delete(Integer collegeId) {
       String sql = "delete from sys_college where college_Id = '"+collegeId+"'";
       getSession().createSQLQuery(sql).executeUpdate();
       SessionClose();
    }

    @Override
    public void edit(Integer collegeId , String collegeName) {
        String sql = "update sys_college set college_name = '"+collegeName+"' where college_id = '"+collegeId+"'";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    public void save(String collegeName) {
        String sql = "insert into sys_college(college_name) value('"+collegeName+"')";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    public SysCollegeEntity findCollegeById(Integer collegeId) {
        Criteria criteria = getSession().createCriteria(SysCollegeEntity.class);
        criteria.add(Restrictions.eq("collegeId",collegeId));
        List<SysCollegeEntity> list = criteria.list();
        SessionClose();
        return list.get(0);
    }
}
